package cn.ecut.dzy.atomics;

import java.util.concurrent.atomic.AtomicIntegerArray;

/**
 * @program: juc
 * @description:
 * @author: DingZhenYun
 * @create: 2021-10-26 22:17
 **/

public class AtomicIntegerArrayDemo {
    public static void main(String[] args) {
        //AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(new int[5]);
        //AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(5);
        AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(new int[]{1,2,3,4,5});

        for (int i = 0; i <atomicIntegerArray.length(); i++) {
            System.out.println(atomicIntegerArray.get(i));
        }
        System.out.println();
        System.out.println();
        System.out.println();
        int tmpInt = 0;

        tmpInt = atomicIntegerArray.getAndSet(0,1122);
        System.out.println(tmpInt+"\t"+atomicIntegerArray.get(0));

        System.out.println( atomicIntegerArray.getAndIncrement(1));//2
        atomicIntegerArray.getAndIncrement(1);//4
        System.out.println( atomicIntegerArray.getAndIncrement(1));//4-->5
        atomicIntegerArray.getAndIncrement(1);//6
        System.out.println( atomicIntegerArray.getAndIncrement(1));//6-->7
        tmpInt = atomicIntegerArray.getAndIncrement(1);//7
        System.out.println( atomicIntegerArray.getAndIncrement(1));
        System.out.println(tmpInt+"\t"+atomicIntegerArray.get(1));

    }
}
